package com.lgy.springboot_example.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

@EnableScheduling
@Component
public class RedisMsgSender {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(initialDelay = 5 * 1000, fixedRate = 6 * 10 * 1000)// 延迟5s，间隔60s，StringRedisTemplate对象向redis消息队列myChat频道发布消息
    public void sendMsgToRedisChat() {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            result.append(new Random().nextInt(10));
        }
        System.out.println("向redis消息队列myChat频道发布消息");
        redisTemplate.convertAndSend("myChat", "myChat content：" + result.toString());
    }

    @Scheduled(initialDelay = 5 * 1000, fixedRate = 6 * 10 * 1000)// 延迟5s，间隔60s，StringRedisTemplate对象向redis消息队列myChat-2频道发布消息
    public void sendMsgToRedisChat2() {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            result.append(new Random().nextInt(10));
        }
        System.out.println("向redis消息队列myChat-2频道发布消息");
        redisTemplate.convertAndSend("myChat-2", "myChat-2 content：" + result.toString());
    }

}
